﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Xml;
namespace 校园网小工具
{
    public partial class ChooseClass : Form
    {
        public ChooseClass()
        {
            InitializeComponent();
        }

        private void ChooseClass_Load(object sender, EventArgs e)
        {
            textBox1.Text = "20142015121811000041339";
            textBox2.Text =  Static.cookie;

        }
        public  bool isWork = true;
        private void button1_Click(object sender, EventArgs e)
        {
            isWork = true;
            if (!backgroundWorker1.IsBusy)
                backgroundWorker1.RunWorkerAsync();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            isWork=false;
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                while (isWork)
                {
                    string strURL = "http://xk.scuec.edu.cn/xsxk/xkOper.xk?method=handleCxxk&jxbid=" + textBox1.Text + "&glJxbid=";
                    HttpWebRequest request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
                    request.Accept = "*/*";
                    string[] cookstr = Static.cookie.Split(';');
                    CookieContainer co = new CookieContainer();
                    foreach (string str in cookstr)
                    {
                        string[] cookieNameValue = str.Split('=');
                        Cookie ck = new Cookie(cookieNameValue[0].Trim().ToString(), cookieNameValue[1].Trim().ToString());
                        ck.Domain = "xk.scuec.edu.cn";
                        co.Add(ck);

                    }
                    request.CookieContainer = co;
                    request.Referer = "http://xk.scuec.edu.cn/xsxk/facxxk.xk";
                    request.Method = "GET";
                    request.KeepAlive = true;
                    request.Headers.Add("Accept-Encoding", "gzip, deflate,sdch");
                    request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36 LBBROWSER";
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);
                    string backstr = sr.ReadToEnd();
                    backgroundWorker1.ReportProgress(0, backstr + "\r\n");

                }
            }
            catch
            {

            }
        }
        int count3 = 0;
        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            try
            {
                if (count3 == 20)
                {
                    richTextBox1.Text += e.UserState;
                    //richTextBox1.Focus();
                    richTextBox1.Select(richTextBox1.Text.Length, 0);
                    //richTextBox1.ScrollToCaret();
                    richTextBox1.Clear();
                    count3 = 0;
                }
                else
                {
                    richTextBox1.Text += e.UserState;
                    //richTextBox1.Focus();
                    richTextBox1.Select(richTextBox1.Text.Length, 0);
                    //richTextBox1.ScrollToCaret();
                    count3++;
                }
            }
            catch
            {

            }
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            richTextBox1.Text +="停止\r\n";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Static.cookie = textBox2.Text;
        }
    }
}
